﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NakedObjects;
using NakedObjects.Services;


namespace HRMDomain
{
    [DisplayName("Employees")]
    public class EmployeeRepository : AbstractFactoryAndRepository
    {
        #region Injected Services
        // This region should contain properties to hold references to any services required by the
        // object.  Use the 'injs' shortcut to add a new service.

        #endregion

        
        public void CreateNewEmployee(string firstName, string lastName, string EmpNo, EmployeeClassType empClass)
        {
            Employee obj = Container.NewTransientInstance<Employee>();
            obj.FirstName = firstName;
            obj.LastName = lastName;
            obj.EmployeeId = EmpNo;
            //obj.ChangeEmployeeClass(empClass,DateTime.Now);
            Container.Persist(ref obj);
            Container.InformUser("New Employee Added.");
        }
        
        public IQueryable<Employee> AllEmployees()
        {
            return Container.Instances<Employee>();
        }

        #region ListLocationEmployees
        public IQueryable<Employee> ListStationEmployees(Location station)
        {
            return from obj in Container.Instances<Employee>()
		                where obj.LocationHistory.Where(w=>w.EndDate==null).Select(s=>s.Location.Id).FirstOrDefault() == station.Id
		                select obj;
        }
        #endregion
        
        #region FindEmployeeByEmployeeNumber
      public Employee FindEmployeeByEmployeeNumber(string empNo)
      {
      var query = from obj in Container.Instances<Employee>()
		              where obj.EmployeeId == empNo
		              select obj;
                  
      return query.FirstOrDefault();
	  //(If inheriting from AbsractFactoryAndRepository can use:) return SingleObjectWarnIfNoMatch(query);
      }
      #endregion
      

    }
}
